package cn.example.jpa.controller;

import cn.example.jpa.entity.Car;
import cn.example.jpa.service.CarserviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cars")
public class CarController {
    @Autowired
    private CarserviceImpl carservice;

    @GetMapping
    public Page<Car> getAll(Car car, int pageNo, int pageSize){
        return carservice.getAll(car,PageRequest.of(pageNo - 1,pageSize));
    }

    @GetMapping("/{id}")
    public Car get(@PathVariable Long id){
        return carservice.get(id);
    }
    @PostMapping
    public Long add(@RequestBody Car car){
        return carservice.add(car);
    }
    @DeleteMapping("/{id}")
    public int del(@PathVariable Long id){
        return carservice.del(id);
    }
    @PutMapping
    public Long update(@RequestBody Car car){
        return carservice.update(car);
    }
}
